package com.wx.app.mylib.app;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import java.util.Map;
import java.util.Set;

/**
 * Created by huangsanm on 2015/4/18.
 */
public class LocalData {

    private Context mContext;

    public LocalData(Context context) {
        mContext = context;
    }

    public void putString(String key, String value) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
        sp.edit().putString(key, value).apply();
    }

    public void putStringSet(String key, Set<String> value) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
        sp.edit().putStringSet(key, value).apply();
    }

    public String getString(String key) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
        return sp.getString(key, "");
    }

    public String getString(String key, String value) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
        return sp.getString(key, value);
    }

    public void putBoolean(String key, boolean value) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
        sp.edit().putBoolean(key, value).apply();
    }

    public boolean getBoolean(String key, boolean defValue) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
        return sp.getBoolean(key, defValue);
    }

    public void putInt(String key, int value) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
        sp.edit().putInt(key, value).apply();
    }

    public int getInt(String key, int value) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
        return sp.getInt(key, value);
    }

    public void putLong(String key, long value) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
        sp.edit().putLong(key, value).apply();
    }

    public long getLong(String key, long value) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
        return sp.getLong(key, value);
    }

    /**
     * 删除key
     *
     * @param key
     */
    public void remove(String key) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
        sp.edit().remove(key).apply();
    }

    /**
     * 根据前缀删除
     *
     * @param prefix
     */
    public void removeByPrefix(String prefix) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
        Map<String, ?> maps = sp.getAll();
        SharedPreferences.Editor e = sp.edit();
        for (String key : maps.keySet()) {
            if (key.startsWith(prefix)) {
                e.remove(key);
            }
        }
        e.apply();
    }

    //声音提醒
    public boolean isVoiceNotify() {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
        return sp.getBoolean(CacheKey.isVoiceNotify, true);
    }

    //震动提醒
    public boolean isVibrateNotify() {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
        return sp.getBoolean(CacheKey.isVibrateNotify, true);
    }

    public static class CacheKey {

        public static final String isVoiceNotify = "isVoiceNotify";
        public static final String isVibrateNotify = "isVibrateNotify";

        //账号id
        public static final String accountId = "accountId";
        //sessionID
        public static final String sessionID = "sessionID";

    }

}
